package com.yh.autobio.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.AdapterView;

import com.yh.autobio.AppContext;
import com.yh.autobio.R;
import com.yh.autobio.api.RetrofitSingleton;
import com.yh.autobio.base.BaseListFragment;
import com.yh.autobio.common.StringUtils;
import com.yh.autobio.common.TDevice;
import com.yh.autobio.common.UIHelper;
import com.yh.autobio.modules.adatper.InvoiceAdapter;
import com.yh.autobio.modules.adatper.ListBaseAdapter;
import com.yh.autobio.modules.bean.InvoiceBean;
import com.yh.autobio.modules.bean.SimpleBackPage;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by john on 2018/3/5 0005. 15:00
 */

public class InvoiceFragment extends BaseListFragment<InvoiceBean> {
    String mVal="";
    @Override
    protected ListBaseAdapter<InvoiceBean> getListAdapter() {
        return new InvoiceAdapter(mActivity);
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        img_shaixuan.setVisibility(View.GONE);
        iv_search.setOnClickListener(this);
        leftDate = new String[]{"收货单位或单据编号"};
        et_earch.setHint(leftDate[0] + "模糊查询");
//        ll_search.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.iv_search:
                TDevice.hideSoftKeyboard(mActivity);
                sendRequestData(null);
                break;
        }
    }

    @Override
    public void inputAction(String str) {
        super.inputAction(str);
        showWaitDialog();
        mCurrentPage = 1;
        mAdapter.clear ();
//        if (!StringUtils.isEmpty(str)){
//            mVal=str;
//        }else {
//            mVal="";
//        }
        sendRequestData(null);
    }


    @Override
    public void searchAction(int position) {
        super.searchAction(position);
        switch (position) {
            case 0:
                et_earch.setHint(leftDate[position] + " 模糊查询");
                break;
            case 1:
                et_earch.setHint(leftDate[position] + " 模糊查询");
                break;
        }
    }

    @Override
    protected void sendRequestData(String id) {
        super.sendRequestData(id);
        showWaitDialog();
        String s = et_earch.getText().toString();
        if (!StringUtils.isEmpty(s)){
            mVal=s;
        }else {
            mVal="";
        }
        RetrofitSingleton.getApiService(mActivity).
                StockAll(mVal,mCurrentPage, AppContext.PAGE_SIZE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        super.onItemClick(parent, view, position, id);
        InvoiceBean item = mAdapter.getItem(position);
        if (item != null) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("ITEM",item);
            UIHelper.showSimpleBack(mActivity, SimpleBackPage.INVOICEDETAIL, bundle, "发货单详情");
        }
    }
}
